package com.kejiguan.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import com.kejiguan.ui.adapter.NeItemSelectAdapter;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.dialog.ModifyNeItemDialog;
import com.kejiguan.ui.dialog.ModifyNeItemDialogUtil;
import com.kejiguan.ui.model.NeItem;
import com.kejiguan.ui.presenter.ChooseNeItemToExhibitionPresenter;
import com.kejiguan.ui.response.NeItemManagerResponse;
import com.kejiguan.ui.response.QueryAllNeItemResponse;
import com.kejiguan.ui.widget.MyLinearLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYY on 16/9/20.
 */
public class SelectNeItemForExhibitionActivity extends Activity implements View.OnClickListener,ModifyNeItemDialog.OnNeItemModifiedListener{


    private ChooseNeItemToExhibitionPresenter mPresenter;
    private RecyclerView neListView;
    private Button chooseBtn;
    private Button backBtn;


    private NeItemSelectAdapter adapter;

    private String exhibitionId;
    private String exhibitionName;
    private String resource;

    private NeItem neItem;
    private NeItem modifyNeItem;


    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        getIntentData();
        mPresenter=new ChooseNeItemToExhibitionPresenter();
        mPresenter.onCreate(SelectNeItemForExhibitionActivity.this);
        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_choose_ne_layout);
        neListView= (RecyclerView) findViewById(R.id.ne_list);
        chooseBtn= (Button) findViewById(R.id.choose_btn);
        backBtn= (Button) findViewById(R.id.back_btn);

        MyLinearLayoutManager layoutManager=new MyLinearLayoutManager(SelectNeItemForExhibitionActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        neListView.setLayoutManager(layoutManager);
        adapter=new NeItemSelectAdapter(SelectNeItemForExhibitionActivity.this,null,SelectNeItemForExhibitionActivity.this);
        neListView.setAdapter(adapter);

        chooseBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        mPresenter.queryNeItems();
    }


    public void getIntentData()
    {
        exhibitionId=getIntent().getStringExtra("exhibitionId");
        exhibitionName=getIntent().getStringExtra("exhibitionName");
        resource=getIntent().getStringExtra("ids");
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.back_btn:
                finish();
                break;
//            case R.id.choose_btn:
//                neItem=adapter.getCurrentNeItem();
//                if (null==neItem)
//                {
//                    Toast.makeText(SelectNeItemForExhibitionActivity.this,"请选择你要添加的网元",Toast.LENGTH_SHORT).show();
//                }else
//                {
//                    CommDialogUtil.getInstance().show(SelectNeItemForExhibitionActivity.this,"将网元"+neItem.getNeName()+"添加到展项"+exhibitionName+"中?",SelectNeItemForExhibitionActivity.this,"添加","取消",0);
//
//                }
//                break;
            case R.id.ne_item_layout:
                int modifyPosition= (int) v.getTag(R.id.recyclerview_item_position);
                modifyNeItem=adapter.getNeItemByPosition(modifyPosition);
                ModifyNeItemDialogUtil.getInstance().show(SelectNeItemForExhibitionActivity.this,modifyNeItem,SelectNeItemForExhibitionActivity.this);
                break;
            case R.id.choose_btn:
                String selectNeIds=adapter.getSelectNeIds();
                Intent resultIntent=new Intent();
                resultIntent.putExtra("ids",selectNeIds);
                resultIntent.putExtra("exhibitionId",exhibitionId);
                setResult(RESULT_OK,resultIntent);
                finish();
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                chooseBtn.setClickable(false);
                mPresenter.attatchNeItem(neItem,exhibitionId);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }

    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryAllNeItemResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            List<NeItem> neItems=response.getEmptyNeItem();
            List<NeItem> neItems1=new ArrayList<>();
            if (null!=neItems)
            {
                for (int i=0;i<neItems.size();i++)
                {
                    if (!TextUtils.isEmpty(resource)&&resource.contains(neItems.get(i).getId()))
                    {
                        neItems.get(i).setSelectForExhibition(true);
                    }else
                    {
                        neItems.get(i).setSelectForExhibition(false);
                    }

                    if (neItems.get(i).getWpaId().equals(exhibitionId))
                    {
                        neItems1.add(neItems.get(i));
                    }
                }
            }
            adapter.setNeItems(neItems1);
        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NeItemManagerResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            switch (response.getOperType())
            {
                case "attach":
                    adapter.addNeItem(response.getNeItem());
                    break;
                case "mod":
                    for (int i=0;i<adapter.getItemCount();i++)
                    {
                        if (adapter.getNeItemByPosition(i).getId().equals(response.getNeItem().getId()))
                        {
                            adapter.setNeItem(i,response.getNeItem());
                            break;
                        }
                    }
                    break;
            }

        }

    }

    @Override
    public void neItemModifiedListener(NeItem item) {
        mPresenter.modifyNeItem(item);
    }
}
